<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\service\AccountService;
use app\admin\service\auth\AdminService;
use app\admin\service\auth\PermissionService;
use app\common\model\system\PluginModel;
use app\common\service\ConfigService;

class Passport extends Controller
{
    protected array $middleware = [
        'admin' => ['except' => ['login']],
    ];

    public function login(AccountService $service)
    {
        $params = $this->request->param(['account' => '', 'password' => '', 'type' => '']);
        $data = $service->login($params['account'], $params['password'], $params['type']);
        if (!$data) {
            return error($service->getError());
        }
        return success('登录成功', $data);
    }

    public function logout()
    {
        return success('退登成功');
    }

    /**
     * 获取当前登录管理员详细信息
     */
    public function profile(AccountService $service)
    {
        $adminId = $this->request->admin_id;
        $userInfo = $service->getAdminInfo($adminId);
        $rules = AdminService::getCodes($adminId);
        $plugins = PluginModel::where('status', 1)->column('name');
        // license
        // $license = ConfigService::get('system', 'license', '');
        return success([
            'userinfo' => $userInfo,
            'rules' => $rules,
            'permission' => (new PermissionService())->getMenu($adminId),
            'plugins' => $plugins,
            'license' => '',
        ]);
    }
}